package cn.zwx.common.rocket.producer.controller;

import cn.zwx.base.exception.BizWrapException;
import cn.zwx.common.rocket.producer.bo.MessageBO;
import cn.zwx.common.rocket.producer.bo.OrderMessageBO;
import cn.zwx.common.rocket.producer.model.OrderEntity;
import cn.zwx.common.rocket.producer.service.DefaultMqProducerService;
import cn.zwx.common.rocket.producer.service.TransactionMqProducerService;
import com.alibaba.fastjson.JSON;
import io.netty.util.concurrent.FastThreadLocalThread;
import io.netty.util.internal.ThreadLocalRandom;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.UUID;

@RestController
public class ProducerController {
    @Resource
    private DefaultMqProducerService defaultMqProducerService;

    @Resource
    private TransactionMqProducerService transactionMqProducerService;

    @RequestMapping("/simple/sendMsg")
    public String sendMsg(String orderNo) throws BizWrapException {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderId(orderNo);
        orderEntity.setOrderStatus(1);
        orderEntity.setOrderDescription("订单"+orderNo);
        double nextDouble = ThreadLocalRandom.current().nextDouble(1000);
        orderEntity.setOrderAmount(new BigDecimal(nextDouble));
        MessageBO messageBO = new MessageBO();
        messageBO.setKey(orderNo);
        messageBO.setMessageBody(JSON.toJSONString(orderEntity));
        messageBO.setTag("*");
        messageBO.setTopic("user_default_topic");
        SendResult sendResult = defaultMqProducerService.sendMsg(messageBO);
        return JSON.toJSONString(sendResult);
    }

    @RequestMapping("/deliverTime/sendMsg")
    public String deliverTimeMsg(String orderNo) throws BizWrapException {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderId(orderNo);
        orderEntity.setOrderStatus(1);
        orderEntity.setOrderDescription("订单"+orderNo);
        double nextDouble = ThreadLocalRandom.current().nextDouble(1000);
        orderEntity.setOrderAmount(new BigDecimal(nextDouble));
        MessageBO messageBO = new MessageBO();
        messageBO.setKey(orderNo);
        messageBO.setMessageBody(JSON.toJSONString(orderEntity));
        messageBO.setTag("*");
        messageBO.setTopic("user_delay_topic");
        // 1s、5s、10s、30s、1m、2m、3m、4m、5m、6m、7m、8m、9m、10m、20m、30m、1h、2h。
        messageBO.setStartDeliverLevel(3);
        SendResult sendResult = defaultMqProducerService.sendMsg(messageBO);
        return JSON.toJSONString(sendResult);
    }


    @RequestMapping("/order/sendMsg")
    public String orderMsg(String orderNo) throws BizWrapException {
        String shardingKey = ThreadLocalRandom.current().nextInt(1000, 10000)+"";
        SendResult sendResult = null;
        for (int i = 0; i < 4; i++) {
            OrderMessageBO messageBO = new OrderMessageBO();
            OrderEntity orderEntity = new OrderEntity();
            orderEntity.setOrderId(Integer.parseInt(orderNo)+i+"");
            orderEntity.setOrderStatus(ThreadLocalRandom.current().nextInt(5));
            orderEntity.setOrderDescription("订单"+Integer.parseInt(orderNo)+i+"");
            double nextDouble = ThreadLocalRandom.current().nextDouble(1000);
            orderEntity.setOrderAmount(new BigDecimal(nextDouble));
            messageBO.setKey(Integer.parseInt(orderNo)+i+"");
            messageBO.setMessageBody(JSON.toJSONString(orderEntity));
            messageBO.setTag("*");
            messageBO.setOrderId(Integer.parseInt(orderNo)+i+"");
            messageBO.setKey(Integer.parseInt(orderNo)+i+"");
            messageBO.setTopic("user_order_topic");
            sendResult = defaultMqProducerService.sendOrderMsg(messageBO,shardingKey);
        }
        return JSON.toJSONString(sendResult);
    }


    @RequestMapping("/trans/sendMsg")
    public String transMsg(String orderNo) throws BizWrapException {
        String shardingKey = ThreadLocalRandom.current().nextDouble(1000, 10000)+"";
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderId(orderNo);
        orderEntity.setOrderStatus(1);
        orderEntity.setOrderDescription("订单"+orderNo);
        double nextDouble = ThreadLocalRandom.current().nextDouble(1000);
        orderEntity.setOrderAmount(new BigDecimal(nextDouble));
        OrderMessageBO messageBO = new OrderMessageBO();
        messageBO.setKey(orderNo);
        messageBO.setMessageBody(JSON.toJSONString(orderEntity));
        messageBO.setTag("*");
        messageBO.setTopic("user_trans_topic");
        SendResult   sendResult = transactionMqProducerService.sendMsg(messageBO,shardingKey);
        return JSON.toJSONString(sendResult);
    }

}
